package com.sykj.why.mongo.service.impl;

import com.sykj.why.exception.WhyException;
import com.sykj.why.mongo.dao.MenuCategoryTDao;
import com.sykj.why.mongo.document.entity.AlbumT;
import com.sykj.why.mongo.document.entity.ContentConfigT;
import com.sykj.why.mongo.document.entity.MenuCategoryT;
import com.sykj.why.mongo.document.entity.MenuT;
import com.sykj.why.mongo.dto.PageDTO;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.AlbumTService;
import com.sykj.why.mongo.service.MenuCategoryTService;
import com.sykj.why.mongo.service.MenuTService;
import com.sykj.why.mongo.vo.entity.MenuCategoryTVo;
import com.sykj.why.mongo.vo.entity.MenuTVo;
import com.sykj.why.util.BaseTools;
import com.sykj.why.util.DateTool;
import com.sykj.why.util.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Eleven
 * @date 2019/6/4
 * @desc 菜单分类业务实现层
 */

@Service("MenuCategoryTService")
public class MenuCategoryTServiceImpl extends BasicServiceImpl<MenuCategoryT> implements MenuCategoryTService {

    @Autowired
    private MenuCategoryTDao menuCategoryTDao;

    @Autowired
    private AlbumTService albumTService;

    @Autowired
    private MenuTService menuTService;

    /**
     * 添加菜单分类
     * @param menuCategoryT
     */
    @Override
    public void saveMenuCategory(MenuCategoryT menuCategoryT) {
        //验证菜单分类名称是否重复
        verifyName(menuCategoryT);
        //排序
        long count = menuCategoryTDao.count(MenuCategoryT.class, false);
        menuCategoryT.setSort((int) count);
        //判断父级是否存在
        //findById(menuCategoryT.getPid());
        //设置时间
        menuCategoryT.setCreateTime(DateTool.getSystemTime());
        menuCategoryT.setUpdateTime(DateTool.getSystemTime());
        //保存
        menuCategoryTDao.save(menuCategoryT);
    }

    /**
     * 删除菜单分类
     * @param id
     */
    @Override
    public void removeMenuCategory(String id) {
        //通过Id查询菜单分类
        MenuCategoryT menuCategoryT = findById(id);
        //逻辑删除菜单分类
        menuCategoryTDao.remove(menuCategoryT);
        //删除子级及菜单分类下的专辑
        removeChildren(id);
        List<AlbumT> list = albumTService.findByMenuCategory(id);
        if (list.size()>0){
            for (AlbumT albumT : list){
                albumTService.removeAlbum(albumT.getId());
            }
        }
    }

    /**
     * 删除子级菜单
     * @param pid
     */
    private void removeChildren(String pid){
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false).and("pid").is(pid));
        List<MenuCategoryT> list = menuCategoryTDao.findAll(query,MenuCategoryT.class);
        if (list.size()>0){
            for (MenuCategoryT menuCategoryT : list){
                removeMenuCategory(menuCategoryT.getId());
                removeChildren(menuCategoryT.getId());
            }
        }
    }

    /**
     * 修改菜单分类
     * @param menuCategoryT
     */
    @Override
    public void updateMenuCategory(MenuCategoryT menuCategoryT) {
        //通过id查询菜单分类
        MenuCategoryT menuCategory = findById(menuCategoryT.getId());
        //重新赋值
        //验证菜单分类名称是否重复
        verifyName(menuCategoryT);
        menuCategory.setCategoryName(menuCategoryT.getCategoryName());
        menuCategory.setUpdateTime(DateTool.getSystemTime());
        //保存或修改
        menuCategoryTDao.saveOrupdate(menuCategory);
    }

    /**
     * 启用/禁用菜单分类
     * @param id
     * @param flag
     */
    @Override
    public void disableOrEnable(String id, boolean flag) {
        //通过id查询菜单分类
        MenuCategoryT menuCategoryT = findById(id);
        //修改状态
        menuCategoryTDao.disableOrEnable(menuCategoryT,flag);
    }

    /**
     * 验证菜单分类名称是否重复
     * @param menuCategoryT
     */
    @Override
    public void verifyName(MenuCategoryT menuCategoryT) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").ne(menuCategoryT.getId()).
                and("menuId").is(menuCategoryT.getMenuId()).
                and("categoryName").is(menuCategoryT.getCategoryName()).and("removed").is(false));
        MenuCategoryT menuCategory = menuCategoryTDao.findOne(query,MenuCategoryT.class);
        if (menuCategory != null) {
            throw new WhyException(ResultEnum.REPEAT);
        }
    }

    /**
     * 通过id查询菜单分类
     * @param id
     * @return
     */
    @Override
    public MenuCategoryT findById(String id) {
        //通过id查询未删除的菜单分类
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id).and("removed").is(false));
        MenuCategoryT menuCategoryT = menuCategoryTDao.findOne(query,MenuCategoryT.class);
        if (menuCategoryT!=null){
            return menuCategoryT;
        }else {
            throw new WhyException(ResultEnum.OBJISNULL);
        }
    }

    /**
     * 查询所有菜单分类
     * @return
     */
    @Override
    public List<MenuCategoryT> findAll() {
        //查询未删除和未禁用的所有菜单分类
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false).and("enable").is(true));
        List<MenuCategoryT> list = menuCategoryTDao.findAll(query,MenuCategoryT.class);
        return list;
    }

    /**
     * 查询所有菜单分类
     * @return
     */
    @Override
    public Page findByPage(PageDTO pageDTO) {
        //查询未删除的菜单分类，并分页传给视图
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false));
        long count = menuCategoryTDao.count(query, MenuCategoryT.class);
        List<MenuCategoryTVo> list = new ArrayList<>();
        for (MenuCategoryT menuCategoryT : menuCategoryTDao.findByPage(query, pageDTO, MenuCategoryT.class)) {
            MenuCategoryTVo vo = new MenuCategoryTVo();
            BeanUtils.copyProperties(menuCategoryT, vo);
            vo.setCreateTime(BaseTools.longToString(menuCategoryT.getCreateTime()));
            vo.setUpdateTime(BaseTools.longToString(menuCategoryT.getUpdateTime()));
            list.add(vo);
        }
        return new Page(pageDTO, count, list);
    }

    /**
     * 通过菜单Id和父级Id查询菜单分类树状图
     * @param menuId
     * @param pid
     * @return
     */
    @Override
    public List<MenuCategoryTVo> findMenuCategoryTreeByMenuIdAndPid(String menuId,String pid) {
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false).
                and("enable").is(true).and("menuId").is(menuId).and("pid").is(pid));
        List<MenuCategoryT> menuCategoryTList = menuCategoryTDao.findAll(query,MenuCategoryT.class);
        List<MenuCategoryTVo> list = new ArrayList<>();
        if (menuCategoryTList.size()>0){
            for (MenuCategoryT menuCategoryT : menuCategoryTList){
                MenuCategoryTVo vo = new MenuCategoryTVo();
                BeanUtils.copyProperties(menuCategoryT, vo);
                if (menuCategoryT.getMenuId()!=null){
                    vo.setMenuName(menuTService.findById(menuCategoryT.getMenuId()).getMenuName());
                }
                vo.setLabel(menuCategoryT.getCategoryName());
                vo.setCreateTime(BaseTools.longToString(menuCategoryT.getCreateTime()));
                vo.setUpdateTime(BaseTools.longToString(menuCategoryT.getUpdateTime()));
                //获取子菜单
                List<MenuCategoryTVo> children = findMenuCategoryTreeByMenuIdAndPid(menuCategoryT.getMenuId(),menuCategoryT.getId());
                if (children!=null){
                    vo.setChildren(children);
                }
                list.add(vo);
            }
            return list;
        }else {
            return null;
        }
    }

    /**
     * 通过菜单Id查询所有菜单分类
     * @param menuId
     * @return
     */
    @Override
    public List<MenuCategoryT> findMenuCategoryListByMenuId(String menuId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false).and("menuId").is(menuId));
        List<MenuCategoryT> list = menuCategoryTDao.findAll(query,MenuCategoryT.class);
        return list;
    }

    /**
     * 查找该菜单分类的所有父级菜单分类
     * @param id
     * @return
     */
    @Override
    public List<MenuCategoryT> findFathers(String id) {
        List<MenuCategoryT> list = new LinkedList<>();
        //获取当前菜单分类
        MenuCategoryT menuCategoryT = findById(id);
        if (menuCategoryT!=null){
            list.add(menuCategoryT);
        }
        if (!menuCategoryT.getPid().equals("0")){
            getFather(list,menuCategoryT.getPid());
        }
        //倒序
        Collections.reverse(list);
        return list;
    }

    private List<MenuCategoryT> getFather(List<MenuCategoryT> list,String id){
        MenuCategoryT menuCategoryT = findById(id);
        if (menuCategoryT!=null){
            list.add(menuCategoryT);
        }
        if (!menuCategoryT.getPid().equals("0")){
            getFather(list,menuCategoryT.getPid());
        }
        return list;
    }

}
